package com.group_g.hkr;

import java.awt.CardLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Panel_Search extends JPanel implements ActionListener{
	

	int extend = 0;
	int extend1 = 0;
	private JButton searchButton;
	private JTextField searchField;
	private JButton clear;
	private Object[] pro;
	private JButton logOut;
	private JPanel panel1;
	private JPanel panel;
	private int c;
	private int b;
	private DB_Connector db;
	
	public Panel_Search(int x, int y, int width, int height) {
		this.setLayout(null);
		this.setBounds(x, y, width, height);
		this.searchButton = setUpSearchButton();
		this.searchField = setUpSearchField();
		this.clear = setUpClearButton();
		this.add(clear);
		this.add(searchField);
		this.add(searchButton);
		logOut = setUpLogOutButton();
		this.add(logOut);
	}

	private JButton setUpSearchButton(){
		JButton temp = new JButton("Search");
		temp.setBounds(350, 20, 200, 30);
		temp.setFont(new Font("Sherif", Font.BOLD, 24));
		temp.addActionListener(this);
		return temp;
	}
	
	private JTextField setUpSearchField(){
		JTextField temp = new JTextField();
		temp.setBounds(20, 20, 300, 30);
		temp.setFont(new Font("Sherif", Font.BOLD, 24));
		temp.addActionListener(this);
		return temp;
	}
	
	private JButton setUpLogOutButton(){
		JButton temp = new JButton("Log out");
		temp.setBounds(750, 20, 100, 30);
		temp.setFont(new Font("Sherif", Font.BOLD, 18));
		temp.addActionListener(this);
		return temp;
	}
	
	private JButton setUpClearButton(){
		JButton temp = new JButton("Clear");
		temp.setBounds(550, 20, 100, 30);
		temp.setFont(new Font("Sherif", Font.BOLD, 18));
		temp.addActionListener(this);
		return temp;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == searchButton) {
			final String searchText = searchField.getText();
			searchField.setText("");
			db = new DB_Connector();
			pro = db.SearchProduct(searchText);
			final Object[] t = pro;
			b =(int) pro[7] + extend;
			panel = new Product((int)pro[0],(String) pro[1],(String) pro[2],(String) pro[3],(String) pro[4],
					(String) pro[5],(int) pro[6],b,(int) pro[8],(int) pro[9]);
			JButton add = setUpADD(searchText, t);
			panel.add(add);
			Panel_Products.getPanel().add(panel);
			Panel_Products.getPanel().revalidate();
			Panel_Products.getPanel().repaint();
			extend+=55;
		}
		if (e.getSource() == logOut){
			extend = 0;
			searchField.setText("");
			Panel_Products.getPanel().removeAll();
			Panel_Basket.getPanel().removeAll();

			Panel_Products.getPanel().revalidate();
			Panel_Products.getPanel().repaint();

			Panel_Basket.getPanel().revalidate();
			Panel_Basket.getPanel().repaint();
			CardLayout cl = (CardLayout) GUI.getMainPanel().getLayout();
			cl.show(GUI.getMainPanel(), GUI.getUserAuthen());
		}
		if (e.getSource() == clear){
			searchField.setText("");
			extend = 0;
			Panel_Products.getPanel().removeAll();
			Panel_Products.getPanel().revalidate();
			Panel_Products.getPanel().repaint();
		}
	}

	private JButton setUpADD(final String searchText, final Object[] t) {
		final JButton add = new JButton("Add");
		add.setBounds(330, 0, 70, 50);
		add.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				c = (int) t[7]+extend1;
				final String productName = (String) t[1];
				panel1 = new Product((int)t[0],(String) t[1],(String) t[2],(String) t[3],(String) t[4],
						(String) t[5],(int) t[6],c,(int) t[8]+100,(int) t[9]);
				JButton remove = setUpREMOVE();
				panel1.add(remove);
				JButton buy = setUpBUY(searchText, t, productName);
				panel1.add(buy);
				Panel_Basket.getPanel().add(panel1);
				Panel_Basket.getPanel().revalidate();
				Panel_Basket.getPanel().repaint();
				extend1+=55;
			}
		});
		return add;
	}

	private JButton setUpBUY(final String searchText, final Object[] t,
			final String productName) {
		JButton buy = new JButton("Buy");
		buy.setBounds(320, 0, 80, 50);
		buy.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String  SQL= "Update product set amount=amount-1 where name=?";
				try {
					db.executeSQL(SQL,productName );
				} catch (Exception e1) {
					e1.printStackTrace();
				}
				
				if (db.checkQuantity(searchText) > 0) {
					Panel_Basket.getPanel().remove(panel1);
					Panel_Products.getPanel().remove(panel);
					Object[] op = db.SearchProduct(searchText);
					panel = new Product((int) op[0], (String) op[1],
							(String) op[2], (String) op[3], (String) op[4],
							(String) op[5], (int) op[6], b, (int) op[8],
							(int) op[9]);
					panel1 = new Product((int) op[0], (String) t[1],
							(String) op[2], (String) op[3], (String) op[4],
							(String) op[5], (int) op[6], c, (int) op[8] + 100,
							(int) op[9]);
					panel.repaint();
					panel.revalidate();
					panel.repaint();
					panel1.revalidate();
					panel.add(setUpADD(searchText, t));
					panel1.add(setUpREMOVE());
					panel1.add(setUpBUY(searchText, t, productName));
					Panel_Products.getPanel().add(panel);
					Panel_Basket.getPanel().add(panel1);
					System.out.println("opaaaaaaaaaaaaaaaaa");
				} else {
					System.out.println("op");
					Panel_Basket.getPanel().remove(panel1);
					Panel_Products.getPanel().remove(panel);
					db.deleteProduct();
				}
				Panel_Basket.getPanel().revalidate();
				Panel_Basket.getPanel().repaint();
				Panel_Products.getPanel().revalidate();
				Panel_Products.getPanel().repaint();
				
			}
		});
		return buy;
	}

	private JButton setUpREMOVE() {
		JButton remove = new JButton("Remove");
		remove.setBounds(400, 0, 100, 50);
		remove.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				Panel_Basket.getPanel().remove(panel1);
				Panel_Basket.getPanel().revalidate();
				Panel_Basket.getPanel().repaint();
				extend1= 0;
			}
		});
		return remove;
	}

}
